Skip to content

docs(label_pr.yml): add labels based on new PR title #1448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025

Conversation

bearomorphism
Copy link
Contributor

@bearomorphism bearomorphism commented May 23, 2025

Description

Not sure if this works as expected, just trying.

The algorithm is not very strict, but I think it's still better than labeling manually by the maintainers.


To add or update labels according to a Pull Request (PR) title, you will need a custom step in your GitHub Actions workflow, because [actions/labeler](https://github.com/actions/labeler) works based on the files changed in the PR, not the PR title.

Here’s how you can update your YAML to include PR title-based labeling using a run: script step:

✅ Updated Workflow YAML

name: "Label Pull Request"
on:
  pull_request_target:

jobs:
  label-pr:
    permissions:
      contents: read
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        sparse-checkout: |
          .github/labeler.yml
        sparse-checkout-cone-mode: false

    - uses: actions/labeler@v5
      with:
        configuration-path: .github/labeler.yml

    - name: Label based on PR title
      uses: actions/github-script@v7
      with:
        script: |
          const title = context.payload.pull_request.title.toLowerCase();
          const labels = [];

          if (title.includes("fix")) {
            labels.push("bug");
          }
          if (title.includes("feature") || title.includes("feat")) {
            labels.push("enhancement");
          }
          if (title.includes("doc") || title.includes("docs")) {
            labels.push("documentation");
          }

          if (labels.length > 0) {
            await github.rest.issues.addLabels({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.payload.pull_request.number,
              labels
            });
          }

🔍 Explanation

  • actions/github-script is used to run custom JavaScript using the GitHub API.
  • You check the PR title (context.payload.pull_request.title) and determine what labels to apply.
  • The addLabels method is used to apply the chosen labels to the PR.

🧩 Tips

  • Adjust the keyword matching logic to fit your naming conventions.
  • This runs on pull_request_target so it will have access to secrets and permissions in the base repo.

Let me know if you'd like to match against regex or support custom config files for title-label mappings.

Copy link

codecov bot commented May 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.62%. Comparing base (120d514) to head (340d8fb).
Report is 626 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1448      +/-   ##
==========================================
+ Coverage   97.33%   97.62%   +0.28%     
==========================================
  Files          42       57      +15     
  Lines        2104     2653     +549     
==========================================
+ Hits         2048     2590     +542     
- Misses         56       63       +7     
Flag Coverage Δ
unittests 97.62% <ø> (+0.28%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bearomorphism bearomorphism changed the title build(label_pr.yml): add labels based on new PR title docs(label_pr.yml): add labels based on new PR title May 23, 2025
@Lee-W Lee-W merged commit 9420b44 into commitizen-tools:master May 23, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants